package com.monster.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/search.action")
public class SearchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = "页码错误！";
        ArrayList<String> lists = new ArrayList<>(16);
        for (int i = 0; i < 16; i++) {
            lists.add("第"+(i+1)+"页内容");
        }
        int page = Integer.parseInt(request.getParameter("page"));
        if (page >= 1 && page <= lists.size()) {
            data = lists.get(page - 1);
        }
        request.getSession().setAttribute("pageTotal", lists.size());

        PrintWriter out = response.getWriter();
        out.print(data);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
